//
//  SelectWebsiteController.m
//  BaoVn
//
//  Created by Le Anh Tai on 12/28/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "SelectWebsiteController.h"
#import "WebsiteCell.h"

@interface SelectWebsiteController ()

@end

@implementation SelectWebsiteController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _alphaView.backgroundColor = [UIColor clearColor];
    _titleLabel.text = @"Chọn Báo";
    [_tableView setAllowsMultipleSelection:YES];
    self.view.hidden = YES;
    [self performSelector:@selector(animateOut) withObject:nil afterDelay:0.1];//make viewWillLayoutSubviews do first
    
    _selectedIndexes = [[NSMutableArray alloc] init];
}

- (void)initializeRightButton
{
    _rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_rightButton setTitle:DONE_TEXT forState:UIControlStateSelected];
    [_rightButton setTitle:DONE_TEXT forState:UIControlStateHighlighted];
    [_rightButton setTitle:DONE_TEXT forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [_rightButton setSelected:YES];
    [_contentView addSubview:_rightButton];
}

- (void)initializeLeftButton
{
    _leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_leftButton setTitle:@"CANCEL" forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_leftButton];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    _leftButton.size = SIZE(80, _rightButton.height);
}

#pragma mark - Logic functions
- (void)prepareData
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"markedWebsite == nil"];
    NSArray *websites = [[CoreData shared] fetchEntity:WEBSITE_ENTITY withPredicate:predicate sortAttribute:TITLE_KEY isAscending:YES];
    if (websites.count == 0)
    {
        return;
    }
    
//    _websites = websites;
}

- (void)saveMarkedWebsite
{
//    NSArray *markedWebsites = [[CoreData shared] fetchEntity:MARKED_WEBSITE_ENTITY];
//    if (markedWebsites.count == 0)
//    {
//        return;
//    }
//    
//    NSManagedObject *markedWebsite = [markedWebsites objectAtIndex:0];
//    
//    for (NSIndexPath *indexPath in _selectedIndexes)
//    {
//        NSManagedObject *website = [_websites objectAtIndex:indexPath.row];
//        [website setValue:markedWebsite forKey:MARKED_WEBSITE_KEY];
//        
//        int order = [[markedWebsite valueForKey:WEBSITE_KEY] count];
//        [website setValue:[NSNumber numberWithInt:order] forKey:ORDER_KEY];
//    }
//    
//    [[CoreData shared] saveContext];
}

#pragma mark - Delegate
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    static NSString *cellIdentifier = @"WebsiteSite";
    WebsiteCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[WebsiteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
//    NSManagedObject *data = [_websites objectAtIndex:indexPath.row];
//    
//    [cell loadData:data];
    
    if ([_selectedIndexes containsObject:indexPath])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    [_selectedIndexes addObject:indexPath];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    [_selectedIndexes removeObject:indexPath];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleNone;
}
#pragma mark -
- (void)cancelButtonPressed
{
    if ([_delegate respondsToSelector:@selector(selectWebsiteDidCancel:)])
    {
        [_delegate selectWebsiteDidCancel:self];
    }
}

- (void)doneButtonPressed
{
    [self saveMarkedWebsite];
    
    if ([_delegate respondsToSelector:@selector(selectWebsiteDidDone:)])
    {
        [_delegate selectWebsiteDidDone:self];
    }
}
@end
